home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#44 (May 89)
/
Notification Source Code
/
HeyYou Source
< prev
next >
Wrap
Text File
|
1989-03-19
|
10KB
|
409 lines
{ HeyYou program for MacTutor by Steve Sheets }
{ Demonstrates uses of the Notificaiton Manager. Creates some simple String Notifications, some Sound }
{ Notifications, some Small Icon Notifications (imediately and after a delay).}
program HeyYou;
{ Resource Constants. }
const
SICNdiamond = 0;
SICNheyyou = 500;
SNDbeep = 1;
SNDclick = 2;
SNDbong = 3;
SNDmonkey = 4;
ALERTabout = 500;
{ Notification Manager Queue Record. }
type
NMRec = record
qLink: QElemPtr;
qType: INTEGER;
nmFlags: INTEGER;
nmPrivate: LONGINT;
nmReserved: INTEGER;
nmMark: INTEGER;
nmSIcon: Handle;
nmSound: Handle;
nmStr: StringPtr;
nmResp: ProcPtr;
nmRefCon: LONGINT;
end;
NMPtr = ^NMRec;
{ Program Global Variables. Menus, Notification Requests, Strings and Flags. }
var
myM1, myM2, myM3: MenuHandle;
StrRec, IconRec: NMRec;
StrStr, IconStr: Str255;
doneFlag, IconFlag: BOOLEAN;
Timer: LONGINT;
{ Notification Manager Glue rotines. }
{ Install Notifiaction Request into Queue. }
function NMInstall (nmReqPtr: QElemPtr): OSErr;
inline
$205F, $A05E, $3E80;
{ MOVE.L (SP)+,A0 }
{ _NMInstall }
{ MOVE.W D0,(SP) }
{ Remove Notifiaction Request into Queue. }
function NMRemove (nmReqPtr: QElemPtr): OSErr;
inline
$205F, $A05F, $3E80;
{ MOVE.L (SP)+,A0 }
{ _NMRemove }
{ MOVE.W D0,(SP) }
{ Checks to see if this is System 6.0 or above. }
function CheckSystem: Boolean;
var
theWorld: SysEnvRec;
begin
if SysEnvirons(1, theWorld) = noErr then
CheckSystem := (theWorld.systemVersion >= $0600)
else
CheckSystem := FALSE;
end;
{ Display About Info. }
procedure DoAbout;
var
dummy: INTEGER;
begin
dummy := Alert(ALERTabout, nil);
end;
{ First Test of Notification Manager. }
{ Displays a simple String. The Notification Request is automatically removed after it is completed. }
procedure NotifStr (St: Str255);
var
E: OSerr;
begin
with StrRec do
begin
qType := 8;
{ No Mark, Icons or Sounds. }
nmMark := 0;
nmSIcon := nil;
nmSound := nil;
{ This String. }
StrStr := St;
nmStr := @StrStr;
{ Automatically Removed when Completed. }
nmResp := POINTER(-1);
nmRefCon := 0;
end;
E := NMInstall(@StrRec);
end;
{ Call by Notification Manager after a Sound Notification has been completed. Removes }
{ the entry from Notification queue, releases SND resource if there is one, disposes of }
{ string handle if there is one and disposes of the request record. }
procedure MySndResponse (nmReqPtr: QElemPtr);
var
aPtr: NMPtr;
E: OSErr;
begin
aPtr := NMPtr(nmReqPtr);
E := NMRemove(nmReqPtr);
if (aPtr^.nmSound <> nil) and (aPtr^.nmSound <> POINTER(-1)) then
ReleaseResource(aPtr^.nmSound);
if aPtr^.nmRefCon <> 0 then
begin
HUnLock(Handle(aPtr^.nmRefCon));
DisposHandle(Handle(aPtr^.nmRefCon));
end;
DisposPtr(Ptr(nmReqPtr));
end;
{ Second Test of Notification Manager. }
{ Displays a String and plays a sound. Allocates memmory for the request record and the string. }
{ If Sound is -1, the System Beep is used. Any other number, indicates a SND resource, and }
{ that resource is loaded in. MySndResponse is setup so that it will be called after the Notification is completed. }
procedure NotifSnd (St: Str255; Sn: INTEGER);
var
E: OSerr;
tempRec: NMPtr;
StrHdl: StringHandle;
begin
tempRec := NMPtr(NewPtr(SIZEOF(NMRec)));
with tempRec^ do
begin
qType := 8;
{ No Marks or Icons. }
nmMark := 0;
nmSIcon := nil;
{ System Beep or SND resource. }
if (Sn = -1) then
nmSound := POINTER(-1)
else
nmSound := GetResource('snd ', Sn);
{ If String, allocate memmory for it. }
if St = '' then
begin
nmStr := nil;
nmRefCon := 0;
end
else
begin
StrHdl := NewString(St);
HLock(Handle(StrHdl));
nmRefCon := ORD4(StrHdl);
nmStr := StrHdl^;
end;
{ Call MySndResponse to remove resource when completed. }
nmResp := @MySndResponse;
end;
E := NMInstall(QElemPtr(tempRec));
end;
{ Third Test of Notification Manager. }
{ Displays a String, displays a Small Icon and Marks the Current Application. }
{ This Notification is not removed until the User selects "Remove Icon" }
{ fromt the Menu or the Application is Quit. }
procedure NotifIcon (St: Str255; IC: INTEGER);
var
E: OSerr;
begin
if not IconFlag then
begin
IconFlag := TRUE;
with IconRec do
begin
qType := 8;
{ Current Application is Marked (in MultiFinder). }
nmMark := 1;
{ Small Icon is used. }
nmSIcon := GetResource('SICN', IC);
{ No SND. }
nmSound := nil;
{ This String. }
{ Use String (if any). }
if St = '' then
nmStr := nil
else
begin
IconStr := St;
nmStr := @IconStr;
end;
{ No Completion Routine. }
nmResp := nil;
nmRefCon := 0;
end;
E := NMInstall(@IconRec);
end;
end;
{ If an small Icon is still flashing, Removes it and releases SND resource. }
procedure RemoveNotifIcon;
var
E: OSerr;
begin
if IconFlag then
begin
IconFlag := FALSE;
E := NMRemove(@IconRec);
if IconRec.nmSIcon <> nil then
ReleaseResource(IconRec.nmSIcon);
end;
end;
{ Normal Mac Setup Procedure }
procedure SetUp;
var
S: Str255;
begin
doneFlag := FALSE;
IconFlag := FALSE;
Timer := 0;
S := '1';
S[1] := CHR(applemark);
myM1 := NewMenu(101, S);
AppendMenu(myM1, 'NotifTest Source;(-');
AddResMenu(myM1, 'DRVR');
InsertMenu(myM1, 0);
myM2 := NewMenu(102, 'File');
AppendMenu(myM2, 'Quit');
InsertMenu(myM2, 0);
myM3 := NewMenu(103, 'Simple');
AppendMenu(myM3, 'String Test #1;String Test #2;(-;Bong Sound Test;Click Sound Test;Bong and Click Sound Test');
AppendMenu(myM3, 'Monkey Sound Alone Test - no alert;System Error Sound Test;(-;Diamond Icon Test');
AppendMenu(myM3, 'HeyYou Icon Alone Test - no alert;Delayed Icon Test;Remove Icon');
InsertMenu(myM3, 0);
DrawMenuBar;
end;
{ Normal Mac Menu Command Routine. }
procedure DoCommand (mResult: LONGINT);
var
theItem: INTEGER;
theMenu: INTEGER;
tempStr: Str255;
tempInteger: INTEGER;
tempLong: LONGINT;
begin
theItem := LoWord(mResult);
theMenu := HiWord(mResult);
case theMenu of
101:
if theItem = 1 then
DoAbout
else
begin
GetItem(myM1, theItem, tempStr);
tempInteger := OpenDeskAcc(tempStr);
end;
102:
if theItem = 1 then
doneFlag := TRUE;
103:
case theItem of
1:
NotifStr('This is a String Test of the Notification Manager.');
2:
begin
GetDateTime(tempLong);
IUTimeString(tempLong, TRUE, tempStr);
tempStr := CONCAT('This is another String Test of the Notification Manager. The Time is ', tempStr);
NotifStr(tempStr);
end;
4:
NotifSnd('This is a Sound Test of the Notification Manager using the Bong sound.', SNDbong);
5:
NotifSnd('This is a Sound Test of the Notification Manager using the Click sound.', SNDclick);
6:
begin
NotifSnd('This is a Sound Test of the Notification Manager using the Bong sound.', SNDbong);
NotifSnd('This is a Sound Test of the Notification Manager using the Click sound.', SNDclick);
end;
7:
NotifSnd('', SNDmonkey);
8:
NotifSnd('This is a Sound Test of the Notification Manager using the System Error sound.', -1);
10:
begin
RemoveNotifIcon;
NotifIcon('This is a Icon Test of the Notification Manager using the Diamond Icon.', SICNdiamond);
end;
11:
begin
RemoveNotifIcon;
NotifIcon('', SICNheyyou);
end;
{ Call NotifIcon after 60 seconds (3600 ticks). }
12:
Timer := TickCount + 3600;
13:
RemoveNotifIcon;
otherwise
end;
otherwise
end;
HiliteMenu(0);
end;
{ Simple Mac Main Event Loop. Check to see if it is time to display HeyYou Icon. }
{ If Resuming under MultiFinder, Remove Icon (if any). }
procedure MainLoop;
const
suspendResumeMessage = 1;
var
myEvent: EventRecord;
whichWindow: WindowPtr;
tempStr: Str255;
tempLong: LONGINT;
begin
repeat
if Timer <> 0 then
if TickCount > Timer then
begin
RemoveNotifIcon;
Timer := 0;
GetDateTime(tempLong);
IUTimeString(tempLong, TRUE, tempStr);
NotifIcon(CONCAT('Hey You! The Time is ', tempStr), SICNheyyou);
end;
if WaitNextEvent(everyEvent, myEvent, 0, nil) then
case myEvent.what of
mouseDown:
case FindWindow(myEvent.where, whichWindow) of
inSysWindow:
SystemClick(myEvent, whichWindow);
inMenuBar:
DoCommand(MenuSelect(myEvent.where));
otherwise
end;
App4Evt:
if BitShift(myEvent.message, -24) = SuspendResumeMessage then
if Odd(myEvent.message) then
RemoveNotifIcon;
otherwise
end;
until doneFlag;
end;
{ Deletes Menus and removes Icon Notification Request (if any). }
procedure CloseDown;
begin
RemoveNotifIcon;
DeleteMenu(101);
DeleteMenu(102);
DeleteMenu(103);
DisposeMenu(myM1);
DisposeMenu(myM2);
DisposeMenu(myM3);
end;
{ Main Program . }
begin
InitGraf(@thePort);
InitFonts;
FlushEvents(everyEvent, 0);
InitWindows;
InitMenus;
TEInit;
InitDialogs(nil);
InitCursor;
DoAbout;
if CheckSystem then
begin
SetUp;
MainLoop;
CloseDown;
end;
end.